const utils = require('../../utils.js')
const config = require('../../config.js')

Component({
  properties: {
    currentItem:Object
  },
  data: {
    times:'',
    voiceContent:'',
    showVoiceText:false,
    playState:false,
    voiceState:true
  },
  methods: {
    // 音频播放
    playFn() {
      if (!!this.data.playState) return;

      const innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.autoplay = true
      innerAudioContext.src = this.data.currentItem['content']
      //开始播放
      innerAudioContext.onPlay(() => {
        this.setData({
          playState: true
        })

        this.setData({
          times: innerAudioContext.duration
        })
      })

      // 播放结束
      innerAudioContext.onEnded((res)=>{
        this.setData({
          playState: false
        })
      })
    },

    // 语音转文字
    recogniseCardContent(e) {
      this.setData({
        showVoiceText: true
      })

      if (this.data.voiceContent !== '') return false;

      wx.showToast({
        title: '语音识别中，请等待！',
        icon: 'loading',
        duration:1000
      })

      utils.ajaxFn({
        url: config.service.recogniseCardContent,
        body: { 'cccId': e.currentTarget.dataset.cccid },
        callback:(data)=> {
          this.setData({
            voiceContent: data.voiceText
          })
        }
      })
    },
    
    // 打卡详情
    ClockFn(e) {
      wx.navigateTo({
        url: `/pages/particulars/particulars?cpcId=${e.currentTarget.dataset.cpcid}&type=${e.currentTarget.dataset.type}`
      })
    }
  },

  attached() {
    // 获取音频长度
    this.setData({
      times: this.data.currentItem['duration']
    })

    this.setData({
      voiceContent: this.data.currentItem['voiceText']
    })

  }
})